using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using WuLibrary;


namespace TowerDefence
{
    public class DrawPath : MonoBehaviour
    {
        public List<Vector3> mMapList = new List<Vector3>();
        private Vector3 mCubeSize=new Vector3(0.98f, 0.98f, 0.98f);

        private void Start()
        {
            mMapList.Add(new Vector3(0,0,0));
            // mMapList.Add(new Vector3(10,0,0));
            mMapList.Add(new Vector3(10,0,10));
            mMapList.Add(new Vector3(5,0,2));
            
        }

        private void Update()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector3 mousePos = Input.mousePosition;
                mousePos.z = 10;
                var pos= Camera.main.ScreenToWorldPoint(mousePos);
               
                pos.x=Mathf.RoundToInt(pos.x);
                pos.z=Mathf.RoundToInt(pos.z);
                
                mMapList.Add(pos);
            }

            if (Input.GetMouseButton(1))
            {
                if (mMapList.Count > 0)
                {
                    mMapList.RemoveAt(mMapList.Count - 1);
                }
            }
            
            if (Input.GetAxis("Mouse ScrollWheel")>0)
            {
                Camera.main.orthographicSize *= 0.9f;
            }

            if (Input.GetAxis("Mouse ScrollWheel") < 0)
            {
                Camera.main.orthographicSize *= 1.1f;
            }

            if (Input.GetKeyDown(KeyCode.A))
            {
                Vector3 pos = Camera.main.transform.localPosition;
                pos.x -= 1;
                Camera.main.transform.localPosition = pos;
            }

            if (Input.GetKeyDown(KeyCode.D))
            {
                Vector3 pos = Camera.main.transform.localPosition;
                pos.x += 1;
                Camera.main.transform.localPosition = pos;
            }

            if (Input.GetKeyDown((KeyCode.W)))
            {
                Vector3 pos = Camera.main.transform.localPosition;
                pos.z += 1;
                Camera.main.transform.localPosition = pos;
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                Vector3 pos = Camera.main.transform.localPosition;
                pos.z-= 1;
                Camera.main.transform.localPosition = pos;
            }

            if (Input.GetKeyDown(KeyCode.F2))
            {
                StringBuilder sb=new StringBuilder();
                for (int i = 0; i<mMapList.Count; i++)
                {
                    sb.AppendLine($"{mMapList[i].x},{mMapList[i].z}");
                }
                
              string filePath=  EditorUtility.SaveFilePanel("保存地图文件",".",DateTime.Now.ToString("yyyyMMddHHmmss") ,"txt");
              File.WriteAllText(filePath,sb.ToString());
            }
        }
        private void OnDrawGizmos()
        {
            if (mMapList.Count == 0) return;
    
            // 起点 - 绿色
            Gizmos.color = Color.green;
            Gizmos.DrawCube(mMapList[0], mCubeSize);
    
            // 路径连线 - 红色，使用改进的绘制方法
            Gizmos.color = Color.red;
            for (int i = 1; i < mMapList.Count; i++)
            {
                DrawPathCubes(mMapList[i-1], mMapList[i]);
            }
    
            // 终点 - 黄色
            Gizmos.color = Color.yellow;
            Gizmos.DrawCube(mMapList[mMapList.Count-1], mCubeSize);
        }
        // 改进的路径绘制方法
        private void DrawPathCubes(Vector3 start, Vector3 end)
        {
            int startX = Mathf.RoundToInt(start.x);
            int startZ = Mathf.RoundToInt(start.z);
            int endX = Mathf.RoundToInt(end.x);
            int endZ = Mathf.RoundToInt(end.z);
    
            // 使用 Bresenham 直线算法绘制连续的立方体
            int dx = Mathf.Abs(endX - startX);
            int dz = Mathf.Abs(endZ - startZ);
            int x = startX;
            int z = startZ;
            int x_inc = (endX > startX) ? 1 : -1;
            int z_inc = (endZ > startZ) ? 1 : -1;
            int error = dx - dz;
    
            for (int i = 0; i <= dx + dz; i++)
            {
                Gizmos.DrawCube(new Vector3(x, 0, z), mCubeSize);
        
                if (error * 2 > -dz)
                {
                    error -= dz;
                    x += x_inc;
                }
                else
                {
                    error += dx;
                    z += z_inc;
                }
            }
        }
        // private void OnDrawGizmos()
        // {
        //     Gizmos.color = Color.green;
        //
        //     if (mMapList.Count > 0)
        //     {
        //         Gizmos.DrawCube(mMapList[0],mCubeSize);
        //     }
        //
        //     int x;
        //     int z;
        //     Gizmos.color = Color.red;
        //     for (int i = 1; i < mMapList.Count; i++)
        //     {
        //         x = (int)mMapList[i-1].x;
        //         z=(int) mMapList[i-1].z;
        //         while (x>mMapList[i].x)
        //         {
        //             x--;
        //             Gizmos.DrawCube(new Vector3(x,0,z),mCubeSize);
        //         }
        //         while (x<mMapList[i].x)
        //         {
        //             x++;
        //             Gizmos.DrawCube(new Vector3(x,0,z),mCubeSize);
        //         }
        //         while (z>mMapList[i].z)
        //         {
        //             z--;
        //             Gizmos.DrawCube(new Vector3(x,0,z),mCubeSize);
        //         }
        //         while (z<mMapList[i].z)
        //         {
        //             z++;
        //             Gizmos.DrawCube(new Vector3(x,0,z),mCubeSize);
        //         }
        //     }
        //    
        //     Gizmos.color = Color.yellow;
        //     if (mMapList.Count > 0)
        //     {
        //         Gizmos.DrawCube(mMapList[mMapList.Count-1],mCubeSize);
        //     }
        //     
        // }
    }
}

